package com.dwyinyuan.interfaces.facade;

import com.dwyinyuan.domain.aggregate_user.service.*;
import com.dwyinyuan.infrastructure.util.enums.*;
import com.dwyinyuan.infrastructure.util.utils.*;
import com.dwyinyuan.interfaces.dto.*;
import com.github.dozermapper.core.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.cache.annotation.*;
import org.springframework.util.*;
import org.springframework.validation.annotation.*;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.*;
import java.util.*;
import java.util.stream.*;


/**
 * 微信公众号：笛舞音缘
 * <p>
 * 用户Controller
 * <p>
 * Created by andy  on 2020-12-16.
 */
@RestController
@RequestMapping("/api/users")
@Validated
@Slf4j
@Api(value = "用户管理Controller", tags = {"用户管理Controller"}, protocols = "http, https", hidden = false)
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ExcelExportService excelExportService;

    @Autowired
    private Mapper mapper;

    /**
     * 新建用户
     */
    @CacheEvict(cacheNames = "users-cache", allEntries = true)
    @PostMapping("/save")
    public ResponseResult save(@Validated(InsertValidationGroup.class) @RequestBody UserDTO userDTO) {
        int save = userService.save(userDTO);

        if (save == 1) {
            return ResponseResult.success("新增成功！");
        } else {
            return ResponseResult.failure(ErrorCodeEnum.INSERT_FAILURE);
        }
    }

    /**
     * 更新用户信息
     *
     * @return
     */
    @PutMapping("/update/{id}")
    @ApiOperation(value = "更新用户信息", notes = "备注说明信息", response = ResponseResult.class, httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "参数说明，用户主键", required = true, paramType = "path", dataType = "Long", example = "12345"),
            @ApiImplicitParam(name = "userDTO", value = "用户信息", required = true, paramType = "body", dataType = "UserDTO", dataTypeClass = UserDTO.class)
    })
    @ApiResponses({@ApiResponse(code = 0000, message = "操作成功"), @ApiResponse(code = 3004, message = "更新失败")})
    public ResponseResult update(@NotNull @PathVariable("id") Long id, @Validated(UpdateValidationGroup.class) @RequestBody UserDTO userDTO) {
        int update = userService.update(id, userDTO);

        if (update == 1) {
            return ResponseResult.success("更新成功！");
        } else {
            return ResponseResult.failure(ErrorCodeEnum.UPDATE_FAILURE);
        }
    }

    /**
     * 删除用户信息
     *
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@NotNull(message = "用户ID不能为空!") @PathVariable("id") Long id) {
        int delete = userService.delete(id);

        if (delete == 1) {
            return ResponseResult.success("删除成功！");
        } else {
            return ResponseResult.failure(ErrorCodeEnum.DELETE_FAILURE);
        }
    }

    /**
     * 查询用户信息
     * GET
     *
     * @return
     */
    @Cacheable(cacheNames = "users-cache")
    @GetMapping("/query")
    public ResponseResult<PageResult> query(@NotNull Integer pageNo, @NotNull Integer pageSize, @Validated UserQueryDTO query) {

        log.info("未使用缓存！");

        // 构造查询条件
        PageQuery<UserQueryDTO> pageQuery = new PageQuery<>();
        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        pageQuery.setQuery(query);

        // 查询
        PageResult<List<UserDTO>> pageResult = userService.query(pageQuery);

        // 实体转换
        List<UserVO> userVOList = Optional.ofNullable(pageResult.getData())
                .map(List::stream)
                .orElseGet(Stream::empty)
                .map(userDTO -> {
                    UserVO userVO = mapper.map(userDTO, UserVO.class);
                    // 对特殊字段做处理
                    userVO.setPassword("******");
                    if (!StringUtils.isEmpty(userDTO.getPhone())) {
                        userVO.setPhone(userDTO.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                    }
                    return userVO;
                })
                .collect(Collectors.toList());

        // 封装返回结果
        PageResult<List<UserVO>> result = new PageResult<>();
        result.setPageNo(pageResult.getPageNo());
        result.setPageNum(pageResult.getPageNum());
        result.setPageSize(pageResult.getPageSize());
        result.setTotal(pageResult.getTotal());
        result.setData(userVOList);

        return ResponseResult.success(result);
    }


    /**
     * 用户数据导出
     *
     * @param query
     * @param filename
     * @return
     */
    @GetMapping("/export")
    public ResponseResult<Boolean> export(@Validated UserQueryDTO query, @NotEmpty String filename) {

        log.info("接受到导出请求！filename = {}", filename);

        // 数据导出
        excelExportService.export(query, filename);
        excelExportService.asyncExport(query, filename);

        return ResponseResult.success(Boolean.TRUE);
    }
}
